/*
 * @lc app=leetcode.cn id=121 lang=golang
 *
 * [121] 买卖股票的最佳时机
 */

// @lc code=start
func maxProfit(prices []int) int {
	l := len(prices)
	if l <= 1 {
		return 0
	} else {
		m := make(map[int]int)
		m[0] = prices[0]
		profit := 0
		for i := 1; i < l; i++ {
			m[i] = min(m[i-1], prices[i])
			profit = max(profit, prices[i]-m[i-1])
		}

		if profit < 0 {
			profit = 0
		}

		return profit
	}
}

func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

// @lc code=end

